package DataBaseModel;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import model.GroupManege;
import model.Time;

//管理qq群等级的数据库模块

public class QQGroupManege {
	/*
	 * 向数据库中插入数据
	 */
	public static boolean insertList(GroupManege gm) throws SQLException {
		Connection con = DataBase.getConn();
		String sql = "insert into QQGroup(gid,uid,score,lastTime,continueDay) values(?,?,?,?,?)";
		PreparedStatement sta = con.prepareStatement(sql);
		sta.setString(1, gm.gid);
		sta.setString(2, gm.uid);
		sta.setInt(3, gm.Score);
		sta.setDate(4, gm.lastTime);
		sta.setInt(5, gm.continueDay);
		int rows = sta.executeUpdate();
		sta.close();
		con.close();
		if (rows > 0) {
			return true;
		} else {
			return false;
		}
	}

	/*
	 * 签到一次
	 */
	public static int[] signIn(String gid, String uid) throws SQLException,
			ParseException {
		Connection con = DataBase.getConn();
		String sql1 = "select * from QQGroup where gid='" + gid + "' and uid='"
				+ uid + "'";

		PreparedStatement sta1 = con.prepareStatement(sql1);
		int[] arr = new int[2];
		int continueDay = 0, score = 0;
		Date lastTime = null;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date nowTime = df.parse(Time.getStringDateShort());// 现在的日期
		int reward = 0;// 奖励分数
		ResultSet rs = sta1.executeQuery();
		// 数据库中没有记录
		if (!rs.next()) {
			GroupManege grm=new GroupManege(gid, uid, 5, new java.sql.Date(nowTime.getTime()) ,1);
			if (insertList(grm)) {
				arr[0] = 1;
				arr[1] = 5;
			}
			return arr;
		}
		lastTime = rs.getDate("lastTime");
		continueDay = rs.getInt("continueDay");
		score = rs.getInt("score");
		System.out.println(lastTime + " " + continueDay + " " + score);

		long diff = nowTime.getTime() - lastTime.getTime();
		long days = diff / (1000 * 60 * 60 * 24);
		// 判断当天是否签到
		if (days == 0) {
			arr[0] = -2;
			arr[1] = score;
			return arr;
		}
		// 判断前一天是否签到
		if (days == 1) {
			reward = ((continueDay + 1) * 5 > 30) ? 30
					: ((continueDay + 1) * 5);
			String sql2 = "update QQGroup set score=?,lastTime=?,continueDay=? where gid=? and uid=?";
			PreparedStatement sta2 = con.prepareStatement(sql2);
			sta2.setInt(1, score + 5 + reward);
			sta2.setDate(2, new java.sql.Date(nowTime.getTime()));
			sta2.setInt(3, continueDay + 1);
			sta2.setString(4, gid);
			sta2.setString(5, uid);
			int i = sta2.executeUpdate();
			if (i > 0) {
				arr[0] = continueDay + 1;
				arr[1] = score + 5 + reward;
				return arr;
			}
			;// 成功签到，返回连续签到天数和分数
			arr[0] = -1;
			arr[1] = score;
			return arr;// 签到失败，返回-1
		} else {
			String sql2 = "update QQGroup set score=?,lastTime=?,continueDay=? where gid=? and uid=?";
			PreparedStatement sta2 = con.prepareStatement(sql2);
			sta2.setInt(1, score + 5);
			sta2.setDate(2, new java.sql.Date(nowTime.getTime()));
			sta2.setInt(3, 1);
			sta2.setString(4, gid);
			sta2.setString(5, uid);
			int i = sta2.executeUpdate();
			if (i > 0) {
				arr[0] = 1;
				arr[1] = score + 5;
				return arr;
			}
			;// 成功签到
			arr[0] = -1;
			arr[1] = score;
			return arr;// 签到失败
		}
	}

	/*
	 * 查询等级
	 */
	public static int searchScore(String gid, String uid) throws SQLException,
			ParseException {
		Connection con = DataBase.getConn();
		String sql = "select * from QQGroup where gid='" + gid + "' and uid='"
				+ uid + "'";
		PreparedStatement sta1 = con.prepareStatement(sql);
		ResultSet rs = sta1.executeQuery();
		int score = 0;
		// 数据库中没有记录
		if (!rs.next()) {
			String sql3 = "insert into QQGroup(gid,uid,score,lastTime,continueDay) values(?,?,?,?,?)";
			PreparedStatement sta3 = con.prepareStatement(sql3);
			sta3.setString(1, gid);
			sta3.setString(2, uid);
			sta3.setInt(3, 0);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date nowTime = df.parse("1997-01-26");
			sta3.setDate(4, new java.sql.Date(nowTime.getTime()));
			sta3.setInt(5, 0);
			int rows = sta3.executeUpdate();
			if (rows > 0) {
				return 0;
			}
		}
		score = rs.getInt("score");
		return score;
	}

	

}
